﻿using Minecraft.BLL;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Cmd
{
	public class PlayerBeAttacked : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Player_PlayerBeAttacked;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<PlayerBeAttackedReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}

			if (req.reduceBloodVolume <= 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：reduceBloodVolume <= 0）" });
				return;
			}
			if (req.point3 == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：point3 == null）" });
				return;
			}
			if (session.minecraftSessionInfo.player.Human_Life <= 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：player.Human_Life <= 0）" });
				return;
			}
			var map = session.minecraftSessionInfo.map;
			if (map.isMoving_Convert)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：map.isBigMoving_Convert）" });
				return;
			}

			session.minecraftSessionInfo.player.Human_Life -= req.reduceBloodVolume;
			if (session.minecraftSessionInfo.player.Human_Life <= 0)
			{
				//死亡
				session.minecraftSessionInfo.player.Human_Life = 0;
				session.minecraftSessionInfo.player.LastDeathTime = DateTimeOffset.Now;
				session.minecraftSessionInfo.player.LastDeathMapId = map.SourceMapId;
				session.minecraftSessionInfo.player.LastDeathPointX = req.point3.x;
				session.minecraftSessionInfo.player.LastDeathPointY = req.point3.y;
				session.minecraftSessionInfo.player.LastDeathPointZ = req.point3.z;

				//将背包、快捷栏、装备的物品移动到尸体的物品栏中
				session.minecraftSessionInfo.goodsList.RemoveAll(m => m.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.PlayerCorpse && m.guid == "");

				if (map.SourceMapId == MinecraftConfig.BaseMapId)
				{
					TakeGoods(session);
				}
				else
				{
					var enumTypeList = new List<EnumGoodsBelongsToType>
					{
						EnumGoodsBelongsToType.ShortcutBar,
						EnumGoodsBelongsToType.Backpack,
						EnumGoodsBelongsToType.Equipment
					};
					session.minecraftSessionInfo.goodsList.RemoveAll(m => enumTypeList.Any(k => k == m.enumGoodsBelongsToType_Convert) && m.guid == "");
				}

				map.LastMapId = map.SourceMapId;
				map.SourceMapId = MinecraftConfig.BaseMapId;
			}
			session.minecraftSessionInfo.goodsList.RemoveAll(m => m.Num <= 0);

			List<KeyValueEnumAttributeInt> attributeList = new List<KeyValueEnumAttributeInt>();
			attributeList.Add(new KeyValueEnumAttributeInt() { key = EnumAttribute.CurHp, value = session.minecraftSessionInfo.player.Human_Life });

			var resp = new PlayerBeAttackedResp
			{
				attributeList = attributeList
			};
			session.Send(defCommand, req, resp);
		}

		private void TakeGoods(MinecraftSession session)
		{
			var enumTypeList = new List<EnumGoodsBelongsToType>
				{
					 EnumGoodsBelongsToType.ShortcutBar,
					  EnumGoodsBelongsToType.Backpack,
					EnumGoodsBelongsToType.Equipment
				};
			var playerGoodsInfo = session.minecraftSessionInfo.goodsList.FirstOrDefault(m => enumTypeList.Any(k => k == m.enumGoodsBelongsToType_Convert) && m.guid == "" && m.Num > 0);
			if (playerGoodsInfo != null)
			{
				var maxCount = session.GetMaxCellNumConvert(playerGoodsInfo.GoodsItemId);
				//开始移动
				var playerCorpseGoodsInfo = session.minecraftSessionInfo.goodsList.FirstOrDefault(m =>
					m.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.PlayerCorpse && m.Num < maxCount);
				int moveCount = 0;
				if (playerCorpseGoodsInfo == null)
				{
					moveCount = Math.Min(maxCount, playerGoodsInfo.Num);
					var newPosition = 0;
					if (session.minecraftSessionInfo.goodsList.Any(m =>
					 m.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.PlayerCorpse))
					{
						newPosition = session.minecraftSessionInfo.goodsList.Count(m =>
						m.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.PlayerCorpse);
					}
					playerCorpseGoodsInfo = new GoodsModel
					{
						BelongsTo = (int)EnumGoodsBelongsToType.PlayerCorpse,
						GoodsId = StringHelper.GetGuidStr(),
						GoodsItemId = playerGoodsInfo.GoodsItemId,
						Num = moveCount,
						guid = "",
						PlayerId = session.minecraftSessionInfo.player.PlayerId,
						WastageValue = playerGoodsInfo.WastageValue,
						GoodsPosition = newPosition,
					};
					session.minecraftSessionInfo.goodsList.Add(playerCorpseGoodsInfo);
				}
				else
				{
					moveCount = Math.Min(maxCount - playerCorpseGoodsInfo.Num, playerGoodsInfo.Num);
					playerCorpseGoodsInfo.Num += moveCount;
				}
				playerGoodsInfo.Num -= moveCount;
				TakeGoods(session);
			}
			else
			{
				//结束移动
			}
		}
	}
}
